package com.lanou.projecta.news;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.projecta.R;
import com.lanou.projecta.tools.StringRequestUtf;

/**
 * Created by dllo on 16/1/16.
 */
public class NewsHeadLineBodyActivityCombination extends AppCompatActivity{
    private ViewPager viewPager;
    private CombinatonPagerAdapter adapter;
    private TextView title;
    private TextView body;
    private RequestQueue queue;

    private BaseCombination baseCombination;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_headline_body_combination);
        viewPager = (ViewPager) findViewById(R.id.combination_viewpager);
        title= (TextView) findViewById(R.id.combination_title);
        body = (TextView) findViewById(R.id.combination_body);
        queue = Volley.newRequestQueue(this);
        Intent intent = getIntent();
        String s = intent.getStringExtra("combination");
        s = s.replace('|','/');
        Log.d("NewsHeadLineBodyActivit", s);
        String url = "http://c.m.163.com/photo/api/set/"+s+".json";

        Log.d("NewsHeadLineBodyActivit", url);


        StringRequestUtf request = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("NewsHeadLineBodyActivit", response);
                baseCombination = new BaseCombination();
                Gson gson = new Gson();
                baseCombination = gson.fromJson(response,BaseCombination.class);
                adapter = new CombinatonPagerAdapter(baseCombination,NewsHeadLineBodyActivityCombination.this);
             viewPager.getCurrentItem();

                title.setText(baseCombination.getSetname());

       viewPager.setAdapter(adapter);
                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        body.setText(baseCombination.getPhotos().get(position).getNote());
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }







}
